package com.springboot.seckill.controller;

import com.springboot.seckill.controller.dto.ItemCreateDTO;
import com.springboot.seckill.controller.viewobject.ItemVO;
import com.springboot.seckill.enums.EmPromoConstants;
import com.springboot.seckill.error.BusinessException;
import com.springboot.seckill.response.CommonReturnType;
import com.springboot.seckill.service.ItemService;
import com.springboot.seckill.service.model.ItemModel;
import org.joda.time.format.DateTimeFormat;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: HuangDekai
 * @Date: 2022/3/18 11:08
 * @Version: 1.0
 * @since: jdk11
 */
@RestController("item")
@RequestMapping("/item")
    //DEFAULT_ALLOWED_HEADERS: 允许跨域传输所有的header参数, 将用于使用token放入header域, 做session共享的跨域请求
    //DEFAULT_ALLOW_CREDENTIALS = true: 需要配合前端设置xhrFields授信后使得跨域session共享
@CrossOrigin(allowCredentials = "true",  allowedHeaders = "*", originPatterns = "*")
public class ItemController extends BaseController{

    @Autowired
    ItemService itemService;

    /**
     * 创建商品的controller
     * @return CommonReturnType
     */
    @PostMapping(value = "/create", consumes= MediaType.APPLICATION_JSON_VALUE)
    public CommonReturnType createItem(@RequestBody @Validated ItemCreateDTO itemCreateDTO) throws BusinessException {

        //封装service请求用来创建商品
        ItemModel itemModel = new ItemModel();
        BeanUtils.copyProperties(itemCreateDTO, itemModel);

        ItemModel itemModelForReturn = itemService.createItem(itemModel);
        ItemVO itemVO = convertItemVOFromModel(itemModelForReturn);
        return CommonReturnType.create(itemVO);
    }

    /**
     * 商品详情页浏览 - 只浏览, 使用Get
     * @param id 商品id
     * @return CommonReturnType(itemVO)
     */
    @GetMapping(value = "/get")
    public CommonReturnType getItem(@PathVariable @RequestParam(name = "id")Integer id) {
        ItemModel itemModel = itemService.getItemById(id);

        ItemVO itemVO = convertItemVOFromModel(itemModel);

        return CommonReturnType.create(itemVO);
    }

    /**
     * 商品列表浏览 - 只浏览, 使用Get
     * @return CommonReturnType(itemVOList)
     */
    @GetMapping(value = "/list")
    public CommonReturnType listItem() {
        List<ItemModel> itemModelList = itemService.listItems();
        List<ItemVO> itemVOList = itemModelList.stream().map(
                this::convertItemVOFromModel
        ).collect(Collectors.toList());
        return CommonReturnType.create(itemVOList);
    }

    /**
     * 将ItemModel转换为ItemVO
     * @param itemModel ItemModel
     * @return ItemVO
     */
    private ItemVO convertItemVOFromModel(ItemModel itemModel){
        if (itemModel == null) {
            return null;
        }
        ItemVO itemVO = new ItemVO();
        BeanUtils.copyProperties(itemModel, itemVO);
        if (itemModel.getPromoModel() != null) {
            //有正在进行或即将进行的秒杀活动
            itemVO.setPromoStatus(itemModel.getPromoModel().getStatus());
            itemVO.setPromoId(itemModel.getPromoModel().getId());
            itemVO.setPromoPrice(itemModel.getPromoModel().getPromoItemPrice());
            itemVO.setStartDate(itemModel.getPromoModel().getStartDate().toString(DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss")));
        } else {
            itemVO.setPromoStatus(EmPromoConstants.SECKILL_STATUS_NOTHING.getValue());
        }
        return itemVO;
    }
}
